﻿using System;
using System.Globalization;
using System.Net;
using System.Windows.Data;

namespace Chess.Client
{
    [ValueConversion(typeof(string), typeof(IPAddress))]
    internal class IpAddressToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ip = value as IPAddress;
            return ip.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            IPAddress address;
            if (IPAddress.TryParse((string)value, out address))
            {
                return address;
            }
            throw new Exception("Невозможно распознать IP адрес сервера");
        }
    }
}
